package com.jumper.study.admin.service.impl;

import java.time.LocalDate;
import java.util.Collection;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.baomidou.mybatisplus.extension.toolkit.SqlHelper;
import com.jumper.study.admin.dto.req.LessonSearchReq;
import com.jumper.study.admin.service.LessonService;
import com.jumper.study.db.dao.LessonMapper;
import com.jumper.study.db.dto.LessonDto;
import com.jumper.study.db.dto.LessonSearchDto;
import com.jumper.study.db.model.Lesson;

@Service
public class LessonServiceImpl extends ServiceImpl<LessonMapper, Lesson> implements LessonService {
    private static final Logger LOGGER = LoggerFactory.getLogger(LessonServiceImpl.class);

    //根据课程名称查找课程
    public Lesson getByTitle(String title, Long id) {
        LambdaQueryWrapper<Lesson> wrapper = Wrappers.lambdaQuery();
        wrapper.eq(Lesson::getTitle, title);
        if (id != null && id > 0) {
            wrapper.ne(Lesson::getId, id);
        }
        return getBaseMapper().selectOne(wrapper);
    }

    @Override
    public boolean recommendById(Long id, boolean recommend) {
        LambdaUpdateWrapper<Lesson> wrapper = Wrappers.lambdaUpdate();
        wrapper.set(Lesson::getRecommend, recommend);
        wrapper.eq(Lesson::getId, id);
        Integer ret = getBaseMapper().update(wrapper);
        return SqlHelper.retBool(ret);
    }

    @Override
    @Transactional
    public boolean recommendBatchById(Collection<Long> ids, boolean recommend) {
        LambdaUpdateWrapper<Lesson> wrapper = Wrappers.lambdaUpdate();
        wrapper.set(Lesson::getRecommend, recommend);
        wrapper.in(Lesson::getId, ids);
        Integer ret = getBaseMapper().update(wrapper);
        return SqlHelper.retBool(ret);
    }

    @Override
    public boolean onselfById(Long id, boolean onself) {
        LambdaUpdateWrapper<Lesson> wrapper = Wrappers.lambdaUpdate();
        wrapper.set(Lesson::getOnself, onself);
        wrapper.eq(Lesson::getId, id);
        Integer ret = getBaseMapper().update(wrapper);
        return SqlHelper.retBool(ret);
    }

    @Override
    @Transactional
    public boolean onselfBatchById(Collection<Long> ids, boolean onself) {
        LambdaUpdateWrapper<Lesson> wrapper = Wrappers.lambdaUpdate();
        wrapper.set(Lesson::getOnself, onself);
        wrapper.in(Lesson::getId, ids);
        Integer ret = getBaseMapper().update(wrapper);
        return SqlHelper.retBool(ret);
    }

    @Override
    public boolean hasLessons(Integer categoryId) {
        LambdaQueryWrapper<Lesson> wrapper = Wrappers.lambdaQuery();
        wrapper.eq(Lesson::getCategoryId, categoryId);
        return getBaseMapper().exists(wrapper);
    }

    //分页读取课程列表
    public IPage<LessonDto> listWithCategory(IPage<LessonDto> page, LessonSearchReq req) {
        LessonSearchDto search = new LessonSearchDto();
        search.setCategoryId(req.getCategoryId());
        search.setTitle(req.getTitle());
        search.setRecommend(req.getRecommend());
        search.setOnself(req.getOnself());
        search.setMinPrice(req.getMinPrice());
        search.setMaxPrice(req.getMaxPrice());
        Integer year = req.getYear();
        if (year != null && year > 2000) {
            search.setStartDate(LocalDate.of(year, 1, 1));
            search.setEndDate(LocalDate.of(year, 12, 31));
            LOGGER.debug("lesson serach date:{}, {}", search.getStartDate(), search.getEndDate());
        }
        return getBaseMapper().selectPageWithCategory(page, search);
    }

    @Override
    public Long lessonCount(Boolean onself) {
        LambdaQueryWrapper<Lesson> query = Wrappers.lambdaQuery();
        if (onself != null) {
            query.eq(Lesson::getOnself, onself);
        }
        return getBaseMapper().selectCount(query);
    }
}
